//
//  ToolsViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/22.
//  Copyright © 2016年 lanou3g. All rights reserved.
//


/**工具页面*/


#import "ToolsViewController.h"
#import "CityViewController.h"
#import "ToolsView.h"
#import "MapController.h"
#import "TravelAccountsViewController.h"
@interface ToolsViewController ()
@property(nonatomic,strong)ToolsView *toolView;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,copy)NSString *temperatureMin;
@property(nonatomic,copy)NSString *temperatureMax;
@property(nonatomic,copy)NSString *time;
@end

@implementation ToolsViewController
- (void)loadView{

    self.toolView = [[ToolsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.toolView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.toolView.chooseBT addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    [self.toolView.bookkeepingBT addTarget:self action:@selector(travelAccountsAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.toolView.mapBT addTarget:self action:@selector(mapAction) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)click{

    CityViewController *controller = [[CityViewController alloc] init];
    controller.currentCityString = @"杭州";
    controller.selectString = ^(NSString *string){
        
        self.toolView.cityLabel.text = string;
        
        [self changeString:self.toolView.cityLabel.text];
        
    };
    [self presentViewController:controller animated:YES completion:nil];
    
}
- (void)changeString:(NSString *)string{

     NSLog(@"%@",string);
    NSMutableString *ms = [[NSMutableString alloc] initWithString:string];
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO)) {
    }
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
    }
    NSString *strUrl = [ms stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *httpUrl = @"http://apis.baidu.com/heweather/weather/free";
    NSString *httpArg = [NSString stringWithFormat:@"city=%@",strUrl];
    [self request:httpUrl withHttpArg:httpArg];

}
-(void)request:(NSString*)httpUrl withHttpArg:(NSString*)HttpArg {
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"bfe08939958b9d825de8ba0891eb0f06" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue]completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
        
        if (error) {
            
            NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
            
        } else {
            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            NSMutableArray *array = dic[@"HeWeather data service 3.0"];
            for (NSDictionary *dic1 in array) {
                NSDictionary *dic2 = dic1[@"basic"];
                NSDictionary *dic3 = dic2[@"update"];
                NSString *str = dic3[@"loc"];
                self.toolView.timeLabel.text = [NSString stringWithFormat:@"当地时间: %@",str];
                NSMutableArray *array = dic1[@"daily_forecast"];
                for (NSDictionary *dic5 in array) {
                    NSDictionary *dic6 = dic5[@"tmp"];
                    self.toolView.minLabel.text = dic6[@"min"];
                    self.toolView.maxLabel.text = dic6[@"max"];
                }
            }
        }
    }];
}

- (void)travelAccountsAction
{
    TravelAccountsViewController *travelAccountsVC = [[TravelAccountsViewController alloc]init];
    
    [self.navigationController pushViewController:travelAccountsVC animated:YES];
    
}

- (void)mapAction{
    MapController *mapVC = [[MapController alloc]init];
    [self.navigationController pushViewController:mapVC animated:YES];
}



@end
